package com.jrx.anytxn.atom.service.impl;

import com.jrx.anytxn.atom.bean.CustBatchSaveBean;
import com.jrx.anytxn.atom.service.BaseBatchAccoutHandle;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.investor.bean.InvestorDealIntrBean;
import com.jrx.anytxn.investor.bean.InvestorIntrAccuBean;
import com.jrx.anytxn.investor.entity.AmInterestAccrualInvestorInfo;
import com.jrx.anytxn.investor.entity.AmLoanAccountInvestorInfo;
import com.jrx.anytxn.investor.entity.AmLoanMainInvestorInfo;
import com.jrx.anytxn.investor.service.IInvestorInterestCalculateService;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: anytxn-cf-parent
 * @description: 联合贷批量-利息计提处理
 * @author: xiaoyu.jing
 * @date: 2020-09-01 17:17
 **/
@Order(150)
@Service
public class InvestorIntrAccrualHandle extends BaseBatchAccoutHandle<InvestorIntrAccuBean> {

    Logger logger = LoggerFactory.getLogger(InvestorIntrAccrualHandle.class);

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    private IInvestorInterestCalculateService interestCalculateService;

    /**
     * 计提业务逻辑处理
     * @return
     * @throws TxnException
     */
    @Override
    public List<InvestorIntrAccuBean> handBatchAccout() throws TxnException {
        CustBatchSaveBean custBatchSaveBean = CustBatchHelper.getCustBatchLocal();
        CmCustomerFirstLevel firstLevel = custBatchSaveBean.getDealCust();
        String tenantId = firstLevel.getTenantId();
        PrOrganizationTable org = organizationTableService.findByOrId(TransBizConstant.DEFAULT_ORG_ID,tenantId);
        Date today = org.getToday();
        Date accruDay = org.getAccruThruDate();//业务机构利息累计日期
        List<AmInterestAccrualInvestorInfo> dealAccuAcctList = custBatchSaveBean.getDealInvestorAccuAcctList();
        if(CollectionUtils.isEmpty(dealAccuAcctList)){
            return null;
        }

        List<InvestorIntrAccuBean> intrAccuBeanList = new ArrayList<>();
        List<AmLoanMainInvestorInfo> dealLoanMainList = custBatchSaveBean.getDealInvestorLoanMainList();
        List<AmLoanAccountInvestorInfo> dealLoanAcctList = custBatchSaveBean.getDealInvestorLoanAccountList();
        for(AmInterestAccrualInvestorInfo accuAcct : dealAccuAcctList){
            String investorLoanId = accuAcct.getInvestorLoanId();
            // 计息日期>=当前日期，表示当天已计息
            if (accuAcct.getAccrualNewToDate() != null && accuAcct.getAccrualNewToDate().compareTo(today) >= 0) {
                logger.debug("不需要进行利息处理,id:{},accountId:{},结息标志:{},计息日期:{}", accuAcct.getId(), accuAcct.getAccountId(), accuAcct.getStopAccrualFlag(), accuAcct.getAccrualNewToDate());
            } else if (TransBizConstant.STOP_ACCRU_FLAG_0.equals(accuAcct.getStopAccrualFlag())) {
                //进行计息处理
                InvestorDealIntrBean dealIntrBean = new InvestorDealIntrBean();
                AmLoanMainInvestorInfo loanMainInfo = dealLoanMainList.stream().filter(loanMain -> investorLoanId.equals(loanMain.getInvestorLoanId())).findFirst().get();
                AmLoanAccountInvestorInfo loanAccountInfo = dealLoanAcctList.stream().filter(loanAcct -> investorLoanId.equals(loanAcct.getInvestorLoanId())).findFirst().get();
                List<AmInterestAccrualInvestorInfo> accrualInfoList = dealAccuAcctList.stream().filter(acct -> investorLoanId.equals(acct.getInvestorLoanId()))
                        .filter(acct -> TransBizConstant.INTEREST_ACCRUAL_TYPE_66.equals(acct.getAccountType()))
                        .filter(acct -> TransBizConstant.STOP_ACCRU_FLAG_1.equals(acct.getStopAccrualFlag())).collect(Collectors.toList());
                dealIntrBean.setLoanAcct(loanAccountInfo);
                dealIntrBean.setLoanMain(loanMainInfo);
                dealIntrBean.setAccuAcct(accuAcct);
                dealIntrBean.setBusinessDate(today);
                dealIntrBean.setAccruDay(accruDay);
                dealIntrBean.setStopAccuAcctList(accrualInfoList);
                InvestorIntrAccuBean intrAccuBean = interestCalculateService.calculateInterest(dealIntrBean);
                intrAccuBeanList.add(intrAccuBean);
            } else if (TransBizConstant.STOP_ACCRU_FLAG_1.equals(accuAcct.getStopAccrualFlag())) {
                logger.debug("不需要进行利息处理,已停止计息,id:{},accountId:{},结息标志:{},计息日期:{}", accuAcct.getId(), accuAcct.getAccountId(), accuAcct.getStopAccrualFlag(), accuAcct.getAccrualNewToDate());
            }
        }
        return intrAccuBeanList;
    }
}
